﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyPoints.Model;

namespace MyPoints.UserControls
{
  /// <summary>
  /// Interaction logic for FilterControl.xaml
  /// </summary>
  public partial class FilterControl : UserControl
  {
    public class VM : NotifyPropertyChanged
    {
      public enum eOption
      {
        Title = 0x1,
        Category = 0x2,
        TitleAndCategory = Title | Category,
        Comment = 0x3,
        Description = 0x4,
        All = Title | Category | Comment | Description
      }

      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
      private string _Expression = "";
      ///<summary>
      /// Sets/gets Expression value. Default value is "".
      ///</summary>
      public string Expression
      {
        get
        {
          return (_Expression);
        }
        set
        {
          _Expression = value;
          OnPropertyChanged(nameof(Expression));
        }
      }

      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
      private bool _IsRegex = false;
      ///<summary>
      /// Sets/gets IsRegex value. Default value is false.
      ///</summary>
      public bool IsRegex
      {
        get
        {
          return (_IsRegex);
        }
        set
        {
          _IsRegex = value;
        }
      }

      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
      private ObservableCollection<eOption> _Options = new ObservableCollection<eOption>();
      ///<summary>
      /// Sets/gets FilterOptions value. Default value is new ObservableCollection<ComboBoxItem>().
      ///</summary>
      public ObservableCollection<eOption> Options
      {
        get
        {
          return (_Options);
        }
        set
        {
          _Options = value;
          OnPropertyChanged(nameof(Options));
        }
      }

      [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
      private eOption _Option = eOption.Title;
      ///<summary>
      /// Sets/gets Option value. Default value is  eOption.Title.
      ///</summary>
      public eOption Option
      {
        get
        {
          return (_Option);
        }
        set
        {
          _Option = value;
          OnPropertyChanged(nameof(Option));
        }
      }

      public VM()
      {
        foreach (eOption v in Enum.GetValues(typeof(eOption)))
        {
          Options.Add(v);
        }
      }
    }

    public delegate void FilterSetDelegate(FilterControl sender, VM filter);
    public event FilterSetDelegate FilterSet;

    [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]
    private VM _Filter = new VM();
    ///<summary>
    /// Sets/gets Filter value. Default value is new VM().
    ///</summary>
    public VM Filter
    {
      get
      {
        return (_Filter);
      }
    }

    private VM vm = new VM();

    public FilterControl()
    {
      InitializeComponent();
      this.DataContext = vm;
    }

    private void btnSetFilter_Click(object sender, RoutedEventArgs e)
    {
      _Filter = new VM() { Expression = vm.Expression, IsRegex = vm.IsRegex, Option = vm.Option };
      FilterSet?.Invoke(this, _Filter);
    }

    private void btnClearFilter_Click(object sender, RoutedEventArgs e)
    {
      _Filter = new VM();
      FilterSet?.Invoke(this, _Filter);
    }
  }
}
